/* bkerndev - Bran's Kernel Development Tutorial
*  By:   Brandon F. (friesenb@gmail.com)
*  Desc: Global function declarations and type definitions
* mode by Roberto Rodriguez for RYMOS
*/
#ifndef __SYSTEM_H
#define __SYSTEM_H
#include "types.h"
#include <stdargs.h>



/* This defines what the stack looks like after an ISR was running */

struct regs
{
    unsigned int gs, fs, es, ds;
    unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
    unsigned int int_no, err_code;
    unsigned int eip, cs, eflags, useresp, ss;    
};

typedef struct
{
  uint32_t ds;             // Data segment selector.
  uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha.
  uint32_t int_no, err_code; // Interrupt number and error code (if applicable).
  uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically.
} registers_t;

/* kernel.c */
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count);
extern size_t strlen(const char *str);
extern void init_video(void);
extern void putch(unsigned char c);
extern void cls();
char* itoa(int val, int base);
void monitor_write_hex(u32int n);
int Total_Alloc_Memory;
u32int MmapZise;

#define ASSERT(b) ((b) ? (void)0 : panic_assert(__FILE__, __LINE__, #b))

/* GDT.C */
extern void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran);
extern void gdt_install();

/* IDT.C */
extern void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags);
extern void idt_install();

/* ISRS.C */
extern void isrs_install();

/* IRQ.C */
extern void irq_install_handler(int irq, void (*handler)(struct regs *r));
extern void irq_uninstall_handler(int irq);
extern void irq_install();

/* TIMER.C */
extern void timer_wait(int ticks);
extern void timer_install(uint32_t frequency);

/* KEYBOARD.C */
extern void keyboard_install();

/*malloc.c*/
//extern void InitMem(int M_f_G, int size_in_bytes);
//extern void * Kalloc(int elem_size);
//extern void Kfree(void *p);
//extern void TestMem();

/*mm.c*/
size_t strlen(const char *str);
size_t strnlen(const char *str, size_t MAX_LEN);
void memmove(void *dest, const void *src, dword count);

/*io.c*/
inline void outportb(unsigned int port, unsigned char value);
inline void outportw(unsigned int port, unsigned int value);
inline unsigned char inportb(unsigned short port);
inline void outportd(word port, dword data);
inline dword inportd(word port);
void outb(uint16_t port, uint8_t value);
uint8_t inb(uint16_t port);
uint16_t inw(uint16_t port);

/*vesa.c*/

/*vsprintf.c*/
static int skip_atoi(const char **s);
int vsprintf(char *buf, const char *fmt, va_list args);
int sprintf(char * buf, const char *fmt, ...);
int printf(char *fmt, ...);
extern void puts(unsigned char *text);

/*support.asm*/
//
// Portability wrappers
//

// bytes = units * 1
void _dosmemgetb(unsigned long addr, unsigned long units, void *buf);
void _dosmemputb(const void *buf, unsigned long units, unsigned long addr);
// bytes = units * 2
void _dosmemgetw(unsigned long addr, unsigned long units, void *buf);
void _dosmemputw(const void *buf, unsigned long units, unsigned long addr);
// bytes = units * 4
void _dosmemgetl(unsigned long addr, unsigned long units, void *buf);
void _dosmemputl(const void *buf, unsigned long units, unsigned long addr);

/*graphic.c*/
int SetPixel(int x, int y, unsigned char color);

/*mouse.c*/
char Mouse_init(void);

/*Hardware.c*/
void Init_HW();

/*panic.c*/
void panic (const char *msg);

/*32Bit Regs*/
typedef union {
  struct {
    unsigned long edi;
    unsigned long esi;
    unsigned long ebp;
    unsigned long res;
    unsigned long ebx;
    unsigned long edx;
    unsigned long ecx;
    unsigned long eax;
  } d;
  struct {
    unsigned short di, di_hi;
    unsigned short si, si_hi;
    unsigned short bp, bp_hi;
    unsigned short res, res_hi;
    unsigned short bx, bx_hi;
    unsigned short dx, dx_hi;
    unsigned short cx, cx_hi;
    unsigned short ax, ax_hi;
    unsigned short flags;
    unsigned short es;
    unsigned short ds;
    unsigned short fs;
    unsigned short gs;
    unsigned short ip;
    unsigned short cs;
    unsigned short sp;
    unsigned short ss;
  } x;
  struct {
    unsigned char edi[4];
    unsigned char esi[4];
    unsigned char ebp[4];
    unsigned char res[4];
    unsigned char bl, bh, ebx_b2, ebx_b3;
    unsigned char dl, dh, edx_b2, edx_b3;
    unsigned char cl, ch, ecx_b2, ecx_b3;
    unsigned char al, ah, eax_b2, eax_b3;
  } h;
} bit_regs;


#define MAKEWORD(a, b)		((unsigned int)(((byte)(b) << 8) + ((byte)(a))))

#endif
